﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Algorithm
{
	[Obsolete("Use Centroid instead")]
	public class CentroidLine
	{
		public void Add(IGeometry geom)
		{
			if (geom is ILineString)
			{
				this.Add(geom.Coordinates);
				return;
			}
			if (geom is IPolygon)
			{
				IPolygon polygon = (IPolygon)geom;
				this.Add(polygon.ExteriorRing.Coordinates);
				for (int i = 0; i < polygon.NumInteriorRings; i++)
				{
					this.Add(polygon.GetInteriorRingN(i).Coordinates);
				}
				return;
			}
			if (geom is IGeometryCollection)
			{
				foreach (IGeometry geom2 in ((IGeometryCollection)geom).Geometries)
				{
					this.Add(geom2);
				}
			}
		}

		public Coordinate Centroid
		{
			get
			{
				return new Coordinate
				{
					X = this._centSum.X / this._totalLength,
					Y = this._centSum.Y / this._totalLength
				};
			}
		}

		public void Add(Coordinate[] pts)
		{
			for (int i = 0; i < pts.Length - 1; i++)
			{
				double num = pts[i].Distance(pts[i + 1]);
				this._totalLength += num;
				double num2 = (pts[i].X + pts[i + 1].X) / 2.0;
				this._centSum.X += num * num2;
				double num3 = (pts[i].Y + pts[i + 1].Y) / 2.0;
				this._centSum.Y += num * num3;
			}
		}

		private readonly Coordinate _centSum = new Coordinate();

		private double _totalLength;
	}
}
